const Base = require('./base.js');
module.exports = class extends Base {
	constructor(ctx) {
		super(ctx);
		this.returns = this.model('store/returns');
		this.delivery = this.model('store/delivery');
		this.order_goods = this.model('store/order_goods');
	}
	async indexAction() {
		if(this.isGet) {
			return this.display();
		}

		let posts = this.post();

		let where = {};
		if(!think.isEmpty(posts.userid)){
			where.user_id = posts.userid;
		}
		const data = await this.returns.dataTable(posts, where);
		console.log(JSON.stringify(data))
		return this.json(data);
	}
	
	async detailAction(){
		if(this.isGet) {
			let gets = this.get();			
			let returns = await this.returns.findOne(gets.id);
			returns.goods = await this.order_goods.findOne(returns.order_goods_id);
			this.assign("returns",returns);
			return this.display();
		}
	}
	
	async dealAction(){
		let posts = this.post();		
		console.log(posts)
		let user = await this.session("user");
		let data = {
			status: posts.status
			
		};
		
		if(posts.status == 1 || posts.status == -2){
			data.deal_time = new Date().valueOf();
			data.deal_idea = posts.deal_idea;
			data.admin_id = user.id
		}
		
		await this.returns.setOne(posts.id,data);
		if(posts.status == 3 ){
			let returns = await this.returns.findOne(posts.id);
			
		await this.delivery.add({
			order_id: returns.order_id,
			order_no: returns.order_no,
			user_id: returns.user_id,
			accept_name: returns.name,
			province_id:returns.province_id,
			city_id:returns.city_id,
			country_id:returns.country_id,
			district_id:returns.district_id,
			address:returns.address,
			zip:returns.zip,
			mobile:returns.mobile,
			status:0,
			remark:posts.deal_idea,
			order_goods_id:returns.order_goods_id,
			create_time:new Date().valueOf()
			
		});
			
		}
		return this.success();
		
	}
};